package com.bana.fvs.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import com.bana.fvs.code.*;
import com.bana.fvs.service.ControllerService;
import com.bana.out.model.GpsConfig;
import com.bana.out.model.LampItem;
import com.bana.out.model.VisibilityScaleItem;
import com.bana.sys.code.SendResult;
import com.bana.sys.code.DatabaseResult;
import com.bana.sys.model.Statistics;
import com.bana.sys.service.CodeService;
import com.bana.fvs.protocol.CollectSocket;
import com.bana.sys.socket.SocketResult;
import com.bana.sys.utils.*;
import com.bana.fvs.dao.*;
import com.bana.fvs.model.*;
import com.bana.fvs.service.FogAreaService;
import org.springframework.web.bind.annotation.*;

/**
 * 雾区
 */
@RestController
@RequestMapping(value = "/fogArea", method = RequestMethod.POST)
public class FogAreaController {

	@Resource
	private FogAreaDAO fogAreaDAO;

	@Resource
	private FogAreaService fogAreaService;

	@Resource
	private ControllerService controllerService;

	@Resource
	private CodeService codeService;

	@Resource
	private VisibilityStatisticsDAO visibilityStatisticsDAO;

	@Resource
	private WeatherStatisticsDAO weatherStatisticsDAO;
	/**
	 * 查询雾区集合
	 * @return
	 */
	@RequestMapping(value = "/getFogAreas.do", method = RequestMethod.POST)
	public List<FogArea> getFogAreas(@RequestBody TokenRequest<Object> request) throws IOException {
		return fogAreaService.getFogAreas(request.getToken());
	}

	/**
	 * chaxun 查询雾区设备信息
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/queryFogAreas.do", method = RequestMethod.POST)
	public List<FogArea> queryFogArea(@RequestBody TokenRequest<Object> request) {
		return fogAreaService.queryFogAreas(request.getToken());
	}


	/**
	 * 新增雾区
	 * @param request 雾区
	 * @return
	 */
	@RequestMapping(value = "/insertFogArea.do", method = RequestMethod.POST)
	public ActionResult insertFogArea(@RequestBody TokenRequest<FogArea> request) {
		ActionResult actionResult=new ActionResult();
		FogArea fogArea=request.getData();
		fogAreaDAO.insertFogArea(fogArea);
		actionResult.setCzjg(DatabaseResult.Success);
		actionResult.setKey(request.getData().getWqbm());
		return actionResult;
	}

	/**
	 * 更新雾区
	 * @param request 雾区
	 * @return
	 */
	@RequestMapping(value = "/updateFogArea.do", method = RequestMethod.POST)
	public ActionResult updateFogArea(@RequestBody TokenRequest<FogArea> request) {
		ActionResult actionResult=new ActionResult();
		fogAreaDAO.updateFogAreaInfo(request.getData());
		actionResult.setCzjg(DatabaseResult.Success);
		return actionResult;
	}

	/**
	 * 更新雾区光感配置
	 * @param request 雾区
	 * @return
	 */
	@RequestMapping(value = "/updateFogAreaLightConfig.do", method = RequestMethod.POST)
	public ActionResult updateFogAreaLightConfig(@RequestBody TokenRequest<FogArea> request) {
		ActionResult actionResult=new ActionResult();
		fogAreaService.updateFogAreaLightConfig(request.getToken(),request.getData());
		actionResult.setCzjg(DatabaseResult.Success);
		return actionResult;
	}

	/**
	 * 更新雾区
	 * @param request 雾区
	 * @return
	 */
	@RequestMapping(value = "/updateFogAreaVisibility.do", method = RequestMethod.POST)
	public ActionResult updateFogAreaVisibility(@RequestBody TokenRequest<FogArea> request) throws IOException {
		ActionResult actionResult=new ActionResult();
		fogAreaService.updateOutVisibility(request.getData().getJcybh(),request.getData().getNjd(),request.getData().getTqdm());
		actionResult.setCzjg(DatabaseResult.Success);
		return actionResult;
	}

	@RequestMapping(value = "/setVisibility.do",method =RequestMethod.POST )
	public List<ActionResult> setVisibility(@RequestBody TokenRequest<FogArea> request) throws IOException {
		ActionResult result=new ActionResult();
		result.setCzjg(DatabaseResult.Success);
		return fogAreaService.setVisibility(request.getToken(),request.getData());
	}

	/**
	 * 删除雾区及相关设备
	 * @param request wqbm 雾区编码
	 * @return
	 */
	@RequestMapping(value = "/deleteFogArea.do", method = RequestMethod.POST)
	public ActionResult deleteFogArea(@RequestBody TokenRequest<FogArea> request) {

		ActionResult actionResult=new ActionResult();
		fogAreaService.deleteFogArea(request.getData());
		actionResult.setCzjg(DatabaseResult.Success);
		return actionResult;
	}

	/**
	 * 查询雾区下的所有设备
	 * @param request 查询参数 wqbm,分页
	 * @return
	 */
    @RequestMapping(value="/getDevices.do",method=RequestMethod.POST)
    public Object getDevices(@RequestBody TokenRequest<FogArea> request){
		return fogAreaService.queryDevices(request.getData().getWqbm());
    }

	/**
	 * 查询设备连接状态
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getDeviceConnections.do",method=RequestMethod.POST)
	public List<CollectSocket.SocketItem> getDeviceConnections(@RequestBody TokenRequest<Object> request) throws IOException {
		return fogAreaService.getDeivceConnections();
	}

	/**
	 * 雾区统计
	 * @param request
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "/getVisibilityStatistics.do", method = RequestMethod.POST)
	public Statistics getVisibilityStatistics(@RequestBody TokenRequest<VisibilityStatisticsParameter> request) throws ParseException {
		VisibilityStatisticsParameter parameter=request.getData();
		Statistics statistics=new Statistics();
		List<Statistics.Series> series=new ArrayList<>();
		List<String> xAxis=new ArrayList<>();

		//按天查询
		if(parameter.getLevel()==VisibilityStatisticsLevel.Day)
		{
			Calendar start=Calendar.getInstance();
			start.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(parameter.getDate()));
			Calendar end=Calendar.getInstance();
			end.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(parameter.getDate()));
			int year=start.get(Calendar.YEAR);
			int month=start.get(Calendar.MONTH);
			int day=start.get(Calendar.DATE);
			//设置横坐标，横坐标为0-23时
			for(int i=0;i<24;++i) {
				xAxis.add(Integer.toString(i));
			}
			for(int wqbm:parameter.getWqbms())
			{
				Statistics.Series item=new Statistics.Series();
				item.setName(Integer.toString(wqbm));
				List<Integer> values=new ArrayList<>();
				List<String> weathers=new ArrayList<>();
				start.set(Calendar.YEAR,year);
				start.set(Calendar.MONTH,month);
				start.set(Calendar.DATE,day);
				end.set(Calendar.YEAR,year);
				end.set(Calendar.MONTH,month);
				end.set(Calendar.DATE,day);
				//设置每个雾区，每个小时的数据，作为y轴数据
				for(int i=0;i<24;++i)
				{
					start.set(Calendar.HOUR_OF_DAY,i);
					end.set(Calendar.HOUR_OF_DAY,i+1);
					String temp1= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(start.getTime());
					String temp2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(end.getTime());
					VisibilityStatistics value=visibilityStatisticsDAO.getVisibilityStatistics(wqbm,
							temp1,
							temp2);
					if(value==null||value.getNjd()<=0)
					{
						values.add(0);
						weathers.add("-");
					}
					else
					{
						values.add(value.getNjd());
						weathers.add(value.getTqdmsm()==null?"-":value.getTqdmsm());
					}
				}
				item.setyAxis(values);
				item.setzAxis(weathers);
				series.add(item);
			}
			statistics.setSeries(series);
			statistics.setxAxis(xAxis);
		}
		//按月
		else if(parameter.getLevel()==VisibilityStatisticsLevel.Month)
		{
			Calendar start=Calendar.getInstance();
			start.setTime(new SimpleDateFormat("yyyy-MM").parse(parameter.getDate()));
			Calendar end=Calendar.getInstance();
			end.setTime(new SimpleDateFormat("yyyy-MM").parse(parameter.getDate()));
			int year=start.get(Calendar.YEAR);
			int month=start.get(Calendar.MONTH);

			Calendar test = Calendar.getInstance();
			test.set(Calendar.YEAR, year);
			test.set(Calendar.MONTH, month);
			test.set(Calendar.DATE, 1);
			test.roll(Calendar.DATE, -1);
			//设置横轴，28~31天
			for(int i=1;i<=test.get(Calendar.DATE);++i)
			{
				xAxis.add(Integer.toString(i));
			}
			for(int wqbm:parameter.getWqbms())
			{
				Statistics.Series item=new Statistics.Series();
				item.setName(Integer.toString(wqbm));
				List<Integer> values=new ArrayList<>();
				List<String> weathers=new ArrayList<>();
				int day=1;
				start.set(Calendar.YEAR,year);
				start.set(Calendar.MONTH,month);
				start.set(Calendar.DAY_OF_MONTH,day);
				end.set(Calendar.YEAR,year);
				end.set(Calendar.MONTH,month);
				end.set(Calendar.DAY_OF_MONTH,day+1);
				//找到每一天的数据作为y轴
				while(month==start.get(Calendar.MONTH))
				{
					VisibilityStatistics value=visibilityStatisticsDAO.getVisibilityStatistics(wqbm,
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(start.getTime()),
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(end.getTime()));
					if(value==null||value.getNjd()<=0)
					{
						values.add(0);
						weathers.add("-");
					}
					else
					{
						values.add(value.getNjd());
						weathers.add(value.getTqdmsm());
					}
					++day;
					start.set(Calendar.DAY_OF_MONTH,day);
					end.set(Calendar.DAY_OF_MONTH,day+1);
				}
				item.setyAxis(values);
				item.setzAxis(weathers);
				series.add(item);
			}
			statistics.setSeries(series);
			statistics.setxAxis(xAxis);
		}
		return statistics;
	}

	/**
	 * 雾区统计
	 * @param request
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "/getDeviceStatistics.do", method = RequestMethod.POST)
	public DeviceStatisticeResult getDeviceStatistics(@RequestBody TokenRequest<Object> request)  {

		Statistics line=new Statistics();
		Statistics pie=new Statistics();
		List<Statistics.Series> lineSeries=new ArrayList<>();
		List<Statistics.Series> pieSeries=new ArrayList<>();
		List<String> xAxis=new ArrayList<>();
		Statistics.Series goodSeries=new Statistics.Series();
		goodSeries.setName(codeService.getCodeValue(CodeType.Sbzt, DeviceStatus.Noraml));
		Statistics.Series errorSeries=new Statistics.Series();
		errorSeries.setName(codeService.getCodeValue(CodeType.Sbzt, DeviceStatus.Error));
		Statistics.Series disconnectionSeries=new Statistics.Series();
		disconnectionSeries.setName(codeService.getCodeValue(CodeType.Wdljzt, LampStatus.Disconnection));
		Statistics.Series doorSeries=new Statistics.Series();
		doorSeries.setName(codeService.getCodeValue(CodeType.Mzt, LampDoorStatus.Steal));
		Statistics.Series tempSeries=new Statistics.Series();
		tempSeries.setName(codeService.getCodeValue(CodeType.Wdzt, LampTemperatureStatus.TooHigh));
		Statistics.Series voltagesSeries=new Statistics.Series();
		voltagesSeries.setName(codeService.getCodeValue(CodeType.Dyzt, LampVoltageStatus.TooLow));

		//每一个故障类型的统计数量
		List<Integer> goods=new ArrayList<>();
		List<Integer> errors=new ArrayList<>();
		List<Integer> disconnections=new ArrayList<>();
		List<Integer> doors=new ArrayList<>();
		List<Integer> temps=new ArrayList<>();
		List<Integer> voltages=new ArrayList<>();

		for(FogArea fogArea:fogAreaService.getFogAreas(request.getToken()))
		{
			//线形图的x周为雾区
			xAxis.add(fogArea.getWqmc());
			int goodCount=0;
			int errorCount=0;
			int disconnection=0;
			int door=0;
			int temp=0;
			int voltage=0;

			for(Controller controller:controllerService.queryControllers(fogArea.getWqbm()))
			{
				List<Lamp> lamps=controllerService.getControllerCache(controller.getKzqbm()).getLamps();
				for(Lamp lamp:lamps)
				{
					if(lamp.getWdljzt()== LampStatus.Disconnection)
					{
						disconnection++;
						errorCount++;
					}
					else if(lamp.getMzt()== LampDoorStatus.Steal)
					{
						door++;
						errorCount++;
					}
					else if(lamp.getWdzt()== LampTemperatureStatus.TooHigh)
					{
						temp++;
						errorCount++;
					}
					else if(lamp.getDyzt()==LampVoltageStatus.TooLow)
					{
						voltage++;
						errorCount++;
					}
					else
					{
						goodCount++;
					}
				}
			}

			goods.add(goodCount);
			errors.add(errorCount);
			disconnections.add(disconnection);
			doors.add(door);
			temps.add(temp);
			voltages.add(voltage);
		}
		goodSeries.setyAxis(goods);
		errorSeries.setyAxis(errors);
		disconnectionSeries.setyAxis(disconnections);
		doorSeries.setyAxis(doors);
		tempSeries.setyAxis(temps);
		voltagesSeries.setyAxis(voltages);

		//线形图的y轴为正常和故障数量
		lineSeries.add(goodSeries);
		lineSeries.add(errorSeries);

		//饼图不存在x轴，以各个故障类型数量作为数据
		pieSeries.add(goodSeries);
		pieSeries.add(disconnectionSeries);
		pieSeries.add(doorSeries);
		pieSeries.add(tempSeries);
		pieSeries.add(voltagesSeries);

		line.setxAxis(xAxis);
		line.setSeries(lineSeries);
		pie.setxAxis(xAxis);
		pie.setSeries(pieSeries);
		DeviceStatisticeResult result=new DeviceStatisticeResult();
		result.setLine(line);
		result.setPie(pie);
		return result;
	}

	@RequestMapping(value = "/getWeatherStatistics.do", method = RequestMethod.POST)
	public List<WeatherStatistics> getWeatherStatistics(@RequestBody TokenRequest<WeatherStatisticsParameter> request)
	{
		return weatherStatisticsDAO.getWeatherStatistics(request.getData().getWqbm(),request.getData().getStartTime(),request.getData().getEndTime());
	}

	/**
     * 切换到主控制器
     * @param request 雾区 jdqbm
     * @return
     * @throws IOException
     */
	@RequestMapping(value="/setRelayMain.do",method=RequestMethod.POST)
	public ActionResult setRelayMain(@RequestBody TokenRequest<FogArea> request) throws IOException {
		SendResult result=fogAreaService.setRelayMain(request.getToken(),request.getData());
		ActionResult actionResult=new ActionResult();
		actionResult.setCzjg(result.ordinal());
		return actionResult;
	}

    /**
     * 切换到备用控制器
     * @param request 雾区 jdqbm
     * @return
     * @throws IOException
     */
	@RequestMapping(value="/setRelayBak.do",method=RequestMethod.POST)
	public ActionResult setRelayBak(@RequestBody TokenRequest<FogArea> request) throws IOException {
		SendResult result=fogAreaService.setRelayBak(request.getToken(),request.getData());
		ActionResult actionResult=new ActionResult();
		actionResult.setCzjg(result.ordinal());
		return actionResult;
	}

    /**
     * 切换到星历控制器
     * @param request 雾区 jdqbm
     * @return
     * @throws IOException
     */
	@RequestMapping(value="/setRelayOff.do",method=RequestMethod.POST)
	public ActionResult setRelayOff(@RequestBody TokenRequest<FogArea> request) throws IOException {
		SendResult result=fogAreaService.setRelayOff(request.getToken(),request.getData());
		ActionResult actionResult=new ActionResult();
		actionResult.setCzjg(result.ordinal());
		return actionResult;
	}
	/**
	 * 上传雾区控制器参数集合
	 * @param request 雾区，wqbm
	 * @return
	 */
	@RequestMapping(value = "/queryControllersConfig.do",method =RequestMethod.POST )
	public List<Controller> queryControllersConfig(@RequestBody TokenRequest<FogArea> request) throws IOException {
		fogAreaService.queryRealControllers(request.getData(),true,false,false,false,false,null,null);
		return request.getData().getControllers_zt();
	}

	/**
	 * 下发雾区控制器参数集合
	 * @param request 雾区 wqbm
	 * @return
	 */
	@RequestMapping(value = "/setControllersConfig.do",method =RequestMethod.POST )
	public List<ActionResult> setControllersConfig(@RequestBody TokenRequest<FogArea> request) throws IOException {
		return fogAreaService.setControllersConfig(request.getToken(),request.getData());
	}

	/**
	 * 上传雾区控制器参数集合
	 * @param request 雾区，wqbm
	 * @return
	 */
	@RequestMapping(value = "/queryControllersTimerConfig.do",method =RequestMethod.POST )
	public List<Controller> queryControllersTimerConfig(@RequestBody TokenRequest<FogArea> request) throws IOException {
		return fogAreaService.queryControllersTimerConfig(request.getData().getWqbm());
	}

	/**
	 * 下发雾区控制器参数集合
	 * @param request 雾区 wqbm
	 * @return
	 */
	@RequestMapping(value = "/setControllersTimerConfig.do",method =RequestMethod.POST )
	public List<ActionResult> setControllersTimerConfig(@RequestBody TokenRequest<Controller> request) throws IOException {
		return fogAreaService.setControllersTimerConfig(request.getData());
	}

	@RequestMapping(value = "/setLeds.do",method =RequestMethod.POST )
	public List<ActionResult> setLeds(@RequestBody TokenRequest<FogArea> request) throws IOException {
		return fogAreaService.setLeds(request.getToken(),request.getData());
	}

	@RequestMapping(value = "/setCameras.do",method =RequestMethod.POST )
	public List<ActionResult> setCameras(@RequestBody TokenRequest<FogArea> request) throws IOException {
		return fogAreaService.setCameras(request.getToken(),request.getData());
	}

	@RequestMapping(value = "/setLights.do",method =RequestMethod.POST )
	public List<ActionResult> controlLights(@RequestBody TokenRequest<FogArea> request) throws IOException {
		return fogAreaService.setWarnings(request.getToken(),request.getData());
	}

	@RequestMapping(value = "/setHorns.do",method =RequestMethod.POST )
	public List<ActionResult> setHorns(@RequestBody TokenRequest<FogArea> request) throws IOException {
		return fogAreaService.setHorns(request.getToken(),request.getData());
	}


	@RequestMapping(value = "/queryFogAreas", method = RequestMethod.POST)
	public List<com.bana.out.model.FogArea> queryFogArea_out(@RequestBody TokenRequest<Object> request) {

		List<FogArea> fogAreas= fogAreaService.queryFogAreas(request.getToken());
		List<com.bana.out.model.FogArea> result=new ArrayList<>();
		for (FogArea fogArea :fogAreas)
		{
			result.add(com.bana.out.model.FogArea.Convert(fogArea));
		}
		return result;
	}

	@RequestMapping(value = "/queryControllersConfig",method =RequestMethod.POST )
	public com.bana.out.model.ControlConfig queryControllersConfig_out(@RequestBody TokenRequest<com.bana.out.model.ControlConfig> request) throws IOException {

		FogArea fogArea=new FogArea();
		fogArea.setWqbm(request.getData().getWqbm());

		fogAreaService.queryRealControllers(fogArea,true,false,true,true,true,null,null);
		com.bana.out.model.ControlConfig result=new com.bana.out.model.ControlConfig();
		result.setWqbm(fogArea.getWqbm());
		if(fogArea.getControllers_zt()==null||fogArea.getControllers_zt().isEmpty())
		{
			return result;
		}
		else
		{
			Controller controller=fogArea.getControllers_zt().get(0);

			FogArea cache=fogAreaService.FogAreaMap.get(fogArea.getWqbm());

			if(cache!=null)
			{
				result.setGzms(cache.getGzms());
				try
				{
					result.setCxsj(Integer.parseInt(cache.getCxsj()));
				}
				catch(Exception e)
				{

				}

				if(controllerService.getWdkz(cache.getKzcl())==controller.getWdkz())
				{
					result.setKzcl(cache.getKzcl());
				}
				else
				{
					result.setKzcl(-controller.getWdkz());
				}
			}


			result.setWdlddj(controllerService.getWdlddj(controller.getWdld()));
			result.setSspl(controllerService.getSspl(controller.getLdsc(),controller.getMdsc()));
			result.setZkb(controllerService.getZkb(controller.getLdsc(),controller.getMdsc()));
			result.setHsjsdzsl(controller.getHsjsdzsl());

			result.setKqwz(controller.getWzmk1()==0);
			result.setSfcl(controller.getSfgs()==1);
			if(controller.getControllerTexts()==null||controller.getControllerTexts().isEmpty())
			{
				result.setWdwz(null);
			}
			else
			{
				result.setWdwz(controller.getControllerTexts().get(0).getWdwz());
			}
		}
		return result;
	}

	@RequestMapping(value = "/setControllersConfig",method =RequestMethod.POST )
	public List<ActionResult> setControllersConfig_out(@RequestBody TokenRequest<com.bana.out.model.ControlConfig> request) throws IOException {

		if(!fogAreaService.checkWqbsm(request.getData().getWqbm(),request.getToken()))
		{
			List<ActionResult> result=new ArrayList<>();
			ActionResult r=new ActionResult();
			r.setCzjg(2);
			result.add(r);
			return result;
		}
		com.bana.out.model.ControlConfig out=request.getData();
		FogArea fogArea=new FogArea();
		fogArea.setWqbm(out.getWqbm());

		fogArea.setGzms(out.getGzms());
		fogArea.setCxsj(Integer.toString(out.getCxsj()));
		fogArea.setKzcl(out.getKzcl());
		fogArea.setWdkz(controllerService.getWdkz(out.getKzcl()));
		fogArea.setWdld(controllerService.getWdld(out.getWdlddj()));
		Tuple<Integer,Integer> t=controllerService.getLdsc(out.getSspl(),out.getZkb());
		fogArea.setLdsc(t.getItem1());
		fogArea.setMdsc(t.getItem2());
		fogArea.setHdys(15);
		fogArea.setMqys(2);
		fogArea.setHsjsdzsl(out.getHsjsdzsl());

		fogArea.setWdlddj(out.getWdlddj());
		fogArea.setSspl(out.getSspl());
		fogArea.setZkb(out.getZkb());

		//bit0
		fogArea.setCfsd(1);
		//bit1
		if(out.isKqwz())
		{
			fogArea.setSfgs(out.isSfcl()?1:0);
		}
		else
		{
			fogArea.setSfgs(0);
		}
		//bit2
		fogArea.setDscf(0);
		//bit3
		fogArea.setQxcf(0);
		//bit4
		fogArea.setWzmk2(1);

		//bit5
		fogArea.setWzmk1(out.isKqwz()?0:1);

		//bit6
		fogArea.setWdgz(0);

		fogArea.setGs_Lddj(5);
		fogArea.setGs_Sspl(1);
		fogArea.setGs_Zkb(1);

		fogArea.setForceTextConfig(true);

		fogArea.setWdwz(out.getWdwz());

		return fogAreaService.setControllersConfig(request.getToken(),fogArea);

	}

	@RequestMapping(value = "/queryControllersTimerConfig",method =RequestMethod.POST )
	public com.bana.out.model.GpsConfig queryControllersTimerConfig_out(@RequestBody TokenRequest<com.bana.out.model.GpsConfig> request) throws IOException {

		List<Controller> controllers=fogAreaService.queryControllersTimerConfig(request.getData().getWqbm());
		com.bana.out.model.GpsConfig config=new com.bana.out.model.GpsConfig();
		config.setWqbm(request.getData().getWqbm());
		if(controllers==null||controllers.isEmpty())
		{
			config.setReadSuccess1(false);
			config.setStartHour1(-1);
			config.setStartMinute1(-1);
			config.setStopHour1(-1);
			config.setStopMinute1(-1);
			config.setMonth1(-1);
			config.setDay1(-1);
			config.setWdkz1(-1);
			config.setWdlddj1(-1);
			config.setZkb1(-1);
			config.setHsjsdzsl1(-1);
			config.setSfsyxl1(false);

			config.setReadSuccess2(false);
			config.setStartHour2(-1);
			config.setStartMinute2(-1);
			config.setStopHour2(-1);
			config.setStopMinute2(-1);
			config.setMonth2(-1);
			config.setDay2(-1);
			config.setWdkz2(-1);
			config.setWdlddj2(-1);
			config.setZkb2(-1);
			config.setHsjsdzsl2(-1);
			config.setSfsyxl2(false);

			config.setReadSuccess3(false);
			config.setStartHour3(-1);
			config.setStartMinute3(-1);
			config.setStopHour3(-1);
			config.setStopMinute3(-1);
			config.setMonth3(-1);
			config.setDay3(-1);
			config.setWdkz3(-1);
			config.setWdlddj3(-1);
			config.setZkb3(-1);
			config.setHsjsdzsl3(-1);
			config.setSfsyxl3(false);
		}
		else
		{
			config.setReadSuccess1(controllers.get(0).isReadSuccess1());
			config.setStartHour1(controllers.get(0).getStartHour1());
			config.setStartMinute1(controllers.get(0).getStartMinute1());
			config.setStopHour1(controllers.get(0).getStopHour1());
			config.setStopMinute1(controllers.get(0).getStopMinute1());
			config.setMonth1(controllers.get(0).getMonth1());
			config.setDay1(controllers.get(0).getDay1());
			config.setWdkz1(controllers.get(0).getWdkz1());
			config.setWdlddj1(controllerService.getWdlddj(controllers.get(0).getWdld1()));
			config.setZkb1(controllers.get(0).getZkb1());
			config.setHsjsdzsl1(controllers.get(0).getHsjsdzsl1());
			config.setSfsyxl1(controllers.get(0).isSfsyxl1());

			config.setReadSuccess2(controllers.get(0).isReadSuccess2());
			config.setStartHour2(controllers.get(0).getStartHour2());
			config.setStartMinute2(controllers.get(0).getStartMinute2());
			config.setStopHour2(controllers.get(0).getStopHour2());
			config.setStopMinute2(controllers.get(0).getStopMinute2());
			config.setMonth2(controllers.get(0).getMonth2());
			config.setDay2(controllers.get(0).getDay2());
			config.setWdkz2(controllers.get(0).getWdkz2());
			config.setWdlddj2(controllerService.getWdlddj(controllers.get(0).getWdld2()));
			config.setZkb2(controllers.get(0).getZkb2());
			config.setHsjsdzsl2(controllers.get(0).getHsjsdzsl2());
			config.setSfsyxl2(controllers.get(0).isSfsyxl2());

			config.setReadSuccess3(controllers.get(0).isReadSuccess3());
			config.setStartHour3(controllers.get(0).getStartHour3());
			config.setStartMinute3(controllers.get(0).getStartMinute3());
			config.setStopHour3(controllers.get(0).getStopHour3());
			config.setStopMinute3(controllers.get(0).getStopMinute3());
			config.setMonth3(controllers.get(0).getMonth3());
			config.setDay3(controllers.get(0).getDay3());
			config.setWdkz3(controllers.get(0).getWdkz3());
			config.setWdlddj3(controllerService.getWdlddj(controllers.get(0).getWdld3()));
			config.setZkb3(controllers.get(0).getZkb3());
			config.setHsjsdzsl3(controllers.get(0).getHsjsdzsl3());
			config.setSfsyxl3(controllers.get(0).isSfsyxl3());
		}
		return config;
	}

	@RequestMapping(value = "/setControllersTimerConfig",method =RequestMethod.POST )
	public List<ActionResult> setControllersTimerConfig_out(@RequestBody TokenRequest<com.bana.out.model.GpsConfig> request) throws IOException {
		if(!fogAreaService.checkWqbsm(request.getData().getWqbm(),request.getToken()))
		{
			List<ActionResult> result=new ArrayList<>();
			ActionResult r=new ActionResult();
			r.setCzjg(2);
			result.add(r);
			return result;
		}
		Controller controller=new Controller();
		controller.setWqbm(request.getData().getWqbm());
		controller.setReadSuccess1(request.getData().isReadSuccess1());
		controller.setStartHour1(request.getData().getStartHour1());
		controller.setStartMinute1(request.getData().getStartMinute1());
		controller.setStopHour1(request.getData().getStopHour1());
		controller.setStopMinute1(request.getData().getStopMinute1());
		controller.setMonth1(request.getData().getMonth1());
		controller.setDay1(request.getData().getDay1());
		controller.setWdkz1(request.getData().getWdkz1());
		controller.setWdld1(controllerService.getWdld(request.getData().getWdlddj1()));
		controller.setZkb1(request.getData().getZkb1());
		controller.setHdys1(15);
		controller.setMqys1(2);
		controller.setHsjsdzsl1(request.getData().getHsjsdzsl1());
		controller.setSfsyxl1(request.getData().isSfsyxl1());

		controller.setReadSuccess2(request.getData().isReadSuccess2());
		controller.setStartHour2(request.getData().getStartHour2());
		controller.setStartMinute2(request.getData().getStartMinute2());
		controller.setStopHour2(request.getData().getStopHour2());
		controller.setStopMinute2(request.getData().getStopMinute2());
		controller.setMonth2(request.getData().getMonth2());
		controller.setDay2(request.getData().getDay2());
		controller.setWdkz2(request.getData().getWdkz2());
		controller.setWdld2(controllerService.getWdld(request.getData().getWdlddj2()));
		controller.setZkb2(request.getData().getZkb2());
		controller.setHdys2(15);
		controller.setMqys2(2);
		controller.setHsjsdzsl2(request.getData().getHsjsdzsl2());
		controller.setSfsyxl2(request.getData().isSfsyxl2());

		controller.setReadSuccess3(request.getData().isReadSuccess3());
		controller.setStartHour3(request.getData().getStartHour3());
		controller.setStartMinute3(request.getData().getStartMinute3());
		controller.setStopHour3(request.getData().getStopHour3());
		controller.setStopMinute3(request.getData().getStopMinute3());
		controller.setMonth3(request.getData().getMonth3());
		controller.setDay3(request.getData().getDay3());
		controller.setWdkz3(request.getData().getWdkz3());
		controller.setWdld3(controllerService.getWdld(request.getData().getWdlddj3()));
		controller.setZkb3(request.getData().getZkb3());
		controller.setHdys3(15);
		controller.setMqys3(2);
		controller.setHsjsdzsl3(request.getData().getHsjsdzsl3());
		controller.setSfsyxl3(request.getData().isSfsyxl3());
		List<ActionResult> results= fogAreaService.setControllersTimerConfig(controller);
		List<ActionResult> temp=new ArrayList<>();
		for(int i=0;i<results.size();)
		{
			int czjg1=results.get(i).getCzjg();
			int czjg2=results.get(i+1).getCzjg();
			int czjg3=results.get(i+2).getCzjg();
			ActionResult result=new ActionResult();
			result.setName(results.get(i).getName());
			if(czjg1==0&&czjg2==0&&czjg3==0)
			{
				result.setCzjg(0);
			}
			else
			{
				result.setCzjg(1);
			}
			temp.add(result);
			i+=3;
		}
		return temp;
	}

	@RequestMapping(value = "/queryController",method =RequestMethod.POST )
	public com.bana.out.model.ControllerText queryController_out(@RequestBody TokenRequest<com.bana.out.model.ControllerText> request) throws IOException {
		List<Controller> controllers=controllerService.getControllers(request.getData().getWqbm());
		com.bana.out.model.ControllerText result=new com.bana.out.model.ControllerText();
		result.setWqbm(request.getData().getWqbm());
		if(controllers==null||controllers.isEmpty())
		{
			result.setControllerTexts(new ArrayList<>());
		}
		else
		{
			controllerService.queryControllerText(controllers.get(0),false,null,null);
			for (ControllerText text : controllers.get(0).getControllerTexts())
			{
				if(text.getLxbh()!=0)
				{
					result.add(text.getLxbh(),text.getWdwz());
				}
			}
		}
		return result;
	}

	@RequestMapping(value = "/setController",method =RequestMethod.POST)
	public List<ActionResult> setController(@RequestBody TokenRequest<com.bana.out.model.ControllerText> request) throws IOException {
		if(!fogAreaService.checkWqbsm(request.getData().getWqbm(),request.getToken()))
		{
			List<ActionResult> result=new ArrayList<>();
			ActionResult r=new ActionResult();
			r.setCzjg(2);
			result.add(r);
			return result;
		}
		List<ActionResult> results=new ArrayList<>();
		List<Controller> controllers=controllerService.getControllers(request.getData().getWqbm());
		for (Controller controller :controllers) {
			List<ControllerText> controllerTexts=new ArrayList<>();
			for(com.bana.out.model.ControllerTextItem item : request.getData().getControllerTexts())
			{
				ControllerText text=new ControllerText();
				text.setLxbh(item.getLxbh());
				text.setWdwz(item.getWdwz());
				controllerTexts.add(text);
			}
			controller.setControllerTexts(controllerTexts);
			Tuple<Integer,Integer> result=controllerService.setControllerText(controller,false,null,null);
			ActionResult actionResult=new ActionResult();
			if(result.getItem2()>0)
			{
				actionResult.setCzjg(1);
			}
			else
			{
				actionResult.setCzjg(0);
			}
			actionResult.setName(controller.getName());
			results.add(actionResult);
		}
		return results;
	}

	@RequestMapping(value = "/queryLamp",method =RequestMethod.POST )
	public com.bana.out.model.Lamp queryLamp(@RequestBody TokenRequest<com.bana.out.model.Lamp> request)  {

		List<Controller> controllers=controllerService.getControllers(request.getData().getWqbm());
		com.bana.out.model.Lamp result=new com.bana.out.model.Lamp();
		result.setWqbm(request.getData().getWqbm());
		List<LampItem> items=new ArrayList<>();
		if(controllers==null||controllers.isEmpty())
		{
			result.setLamps(new ArrayList<>());
		}
		else
		{
			Controller c=controllerService.getControllerCache(controllers.get(0).getKzqbm());
			if(c==null||c.getLamps()==null||c.getLamps().isEmpty())
			{
				result.setLamps(new ArrayList<>());
			}
			else{
				for (Lamp lamp : c.getLamps()) {
					LampItem item=new LampItem();
					item.setWdbm(lamp.getWdbm());
					item.setDyzt(lamp.getDyzt());
					item.setLdzt(lamp.getLdzt());
					item.setLedzt(lamp.getLedzt());
					item.setTyndy(lamp.getTyndy());
					item.setWdwd(lamp.getWdwd());
					item.setWdzt(lamp.getWdzt());
					items.add(item);
				}
			}
			result.setLamps(items);
		}
		return result;
	}

	@RequestMapping(value = "/queryVisibilityScales",method =RequestMethod.POST)
	public com.bana.out.model.VisibilityScale queryVisibilityScales(@RequestBody TokenRequest<com.bana.out.model.VisibilityScale> request) throws IOException {

		List<Controller> controllers=controllerService.getControllers(request.getData().getWqbm());
		com.bana.out.model.VisibilityScale result=new com.bana.out.model.VisibilityScale();
		result.setWqbm(request.getData().getWqbm());
		List<VisibilityScaleItem> items=new ArrayList<>();
		if(controllers==null||controllers.isEmpty())
		{
			result.setVisibilityScales(new ArrayList<>());
		}
		else
		{
			List<VisibilityScale> visibilityScales=controllerService.queryVisibilityScales(request.getToken(),controllers.get(0).getKzqdk(),controllers.get(0).getKzqbsm());
			for (VisibilityScale scale :visibilityScales) {

				VisibilityScaleItem item=new VisibilityScaleItem();
				item.setNjddj(scale.getNjddj());
				item.setNjdsx(scale.getNjdsx());
				item.setWdkz(scale.getWdkz());
				item.setWdlddj(controllerService.getWdlddj(scale.getWdld()));
				item.setSspl(controllerService.getSspl(scale.getLdsc(),scale.getMdsc()));
				item.setZkb(controllerService.getZkb(scale.getLdsc(),scale.getMdsc()));
				item.setHsjsdzsl(scale.getHsjsdzsl());
				items.add(item);
			}
			result.setVisibilityScales(items);
		}
		return result;
	}

	@RequestMapping(value = "/setVisibilityScales",method =RequestMethod.POST)
	public List<ActionResult> setVisibilityScales(@RequestBody TokenRequest<com.bana.out.model.VisibilityScale> request) throws IOException {
		if(!fogAreaService.checkWqbsm(request.getData().getWqbm(),request.getToken()))
		{
			List<ActionResult> result=new ArrayList<>();
			ActionResult r=new ActionResult();
			r.setCzjg(2);
			result.add(r);
			return result;
		}
		List<ActionResult> list=new ArrayList<>();
		List<VisibilityScale> visibilityScales=new ArrayList<>();
		for (com.bana.out.model.VisibilityScaleItem item : request.getData().getVisibilityScales()) {
			VisibilityScale scale=new VisibilityScale();
			scale.setNjddj(item.getNjddj());
			scale.setNjdsx(item.getNjdsx());
			scale.setWdkz(item.getWdkz());
			scale.setWdld(controllerService.getWdld(item.getWdlddj()));
			Tuple<Integer,Integer> t=controllerService.getLdsc(item.getSspl(),item.getZkb());
			scale.setLdsc(t.getItem1());
			scale.setMdsc(t.getItem2());
			scale.setHdys(15);
			scale.setMqys(2);
			scale.setHsjsdzsl(item.getHsjsdzsl());
			visibilityScales.add(scale);
		}
		List<Controller> controllers=controllerService.getControllers(request.getData().getWqbm());
		for (Controller controller :controllers)
		{
			SocketResult result=controllerService.setVisibilityScale(Token.SystemToken,controller,visibilityScales);
			ActionResult actionResult=new ActionResult();
			if(result.getResult()==SendResult.Success)
			{
				actionResult.setCzjg(0);
			}
			else
			{
				actionResult.setCzjg(1);
			}
			actionResult.setName(controller.getKzqmc());
			list.add(actionResult);
		}

		return list;
	}
}